package com.lp.user.service.impl;

import com.lp.common.Result;
import com.lp.common.base.BaseService;
import com.lp.common.dto.PageBase;
import com.lp.user.dao.ProductMapper;
import com.lp.user.dto.Product;
import com.lp.user.dto.User;
import com.lp.user.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangzhiqiang
 * @Date: 2019/4/18 15:44
 * @Desc:
 */
@Service
public class ProductServiceImpl extends BaseService implements ProductService {
    @Autowired
    private ProductMapper productMapper;


    @Override
    public Result getProductInfo(Integer id) {
        Result result = new Result();
        Product product = productMapper.selectByPrimaryKey(id);
        result.setData(product);
        return result;
    }

    @Override
    public Result getProductInfos(Map param) {
        Result r = new Result();
        List<Product> data = productMapper.getProductInfos(param);
        r.setData(data);
        return r;
    }



    @Override
    public Result saveProductInfo(Product product) {
//        product.setId(1);
        product.setInsertTime(new Date());
        product.setUpdateTime(new Date());
        int i = productMapper.insertSelective(product);

        Result r = new Result();
        if (i == 0) {
            r.setSuccess(SUCCESS);
        } else {
            r.setSuccess(FAIL);
        }
        r.setData(product);
        return r;
    }

    @Override
    public int saveProductInIsolatedTx(Product product) {
        return 0;
    }

    @Override
    public Result updateProductInfo(Product product) {
        int i = productMapper.updateByPrimaryKeySelective(product);
        Result result = new Result();
        if (i > 0) {
            result.setSuccess(SUCCESS);
        } else {
            result.setSuccess(FAIL);
        }
        return result;
    }

    @Override
    public Result getPage(Integer currentPage) {
        Result r=new Result();
        PageBase<Product> dt=new PageBase<Product>();
        dt.setCurrentPage(currentPage);
        Map p = new HashMap();
        p.put("startIndex",dt.getStartIndex());
        p.put("pageSize",10);
        int count=productMapper.getCount(p);
        List<Product> u= productMapper.getPageByParam(p);
        dt.setData(u);
        dt.setCount(count);
        r.setData(dt);
        return r;
    }

    @Override
    public Result getList() {
        return null;
    }
}
